package com.cskaoyan.config5;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @EnableConfigurationProperties注解
 * @author stone
 * @date 2022/10/10 16:10
 */
@Configuration
@EnableConfigurationProperties(DataSourceProperties5.class) //参数类上不需要@Component注解
public class DataSourceConfiguration5 {

    /*@Autowired
    DataSourceProperties5 properties5;*/

    DataSourceProperties5 properties5;
    // 有参构造方法的形参，按照类型从容器中取出组件
    public DataSourceConfiguration5(DataSourceProperties5 properties5) {
        this.properties5 = properties5;
    }

    @Bean
    public DataSource dataSource5() {
        DruidDataSource dataSource = new DruidDataSource();
        // ctrl + alt + v
        dataSource.setDriverClassName(properties5.getDriverClassName());
        dataSource.setUrl(properties5.getUrl());
        dataSource.setUsername(properties5.getUsername());
        dataSource.setPassword(properties5.getPassword());
        return dataSource;
    }
}
